<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Banners extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('Query_builder_model', 'qb');
        $param = array(
            'controller' => $this->controller
        );
        $this->load->library('User', $param, 'u');
        
        $this->u->auth();
        
        $this->load->library('Crud', $param, 'cd');
        $this->load->library('Template', $param, 'tpl');
        $this->load->library('File_manipulate', $param, 'fm');
        $this->load->model('Pagination_model', 'pg');
    }

    private $controller = 'banners';

    public function index($action='list', $id=0) {

        $res = '';
        $this->tpl->set('ckeditor', false);
        $ajax = $this->input->post('ajax');

        switch ($action) {
            case 'add':
                $res = $this->cd->c_add();
                break;
            case 'edit':
                $res = $this->cd->c_edit($id);
                break;
            case 'save':
                $id = $this->input->post('id');
                $res = $this->save($id);
                break;
            default :
                $this->tpl->set('js_exeption', array('jquery.validate.min'));
                $this->tpl->set('fancybox', true);
                $this->tpl->set('js', array('fancybox_init'),true);
                
                $res = $this->cd->c_listing($id);
                if ($ajax) {
                    exit(json_encode(array('html' => $res)));
                }
                break;
        }


        if (in_array($action, array('add', 'edit'))) {
            $this->tpl->set('jquery_ui', true);
            $this->tpl->initialize(array('js_opt' => $this->js_opt()));
        }

        $this->tpl->initialize(array('current_page' => $res));
        echo $this->tpl->render();
    }
    
    function sort($field, $order='ASC') {
        $this->cd->set_sort(array('field' => $field, 'order' => $order));
        $this->index();
    }
    
    function save($id=0){
        $this->cd->c_save($id,true);
        $param = array(
            'success' => 1,
            'redirect' => BASE_URL . 'banners/'
        );
        exit (json_encode($param));
    }

    function js_opt() {
        $ret = js(IMPORTS_URL . 'js/image_selector');
        return $ret;
    }

    function image_selector() {
        $array = array(
            'html' => $this->get_image_selector()
        );
        echo json_encode($array);
    }

    private function get_image_selector() {
        $query = array(
            'from' => 'files',
            'where' => array('user' => $this->u->id)
        );
        $images = $this->qb->select($query);

        $query = array(
            'from' => 'pages',
            'where' => array('user' => $this->u->id)
        );
        
        $pages = $this->qb->select($query);
        
        $param = array(
            'images' => $images,
            'pages' => $pages
        );
        
        return $this->load->view('crud/image_selector', $param, TRUE);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */